package web.servlet;


import domain.Product;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.CatelogService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SearchProductsFormServlet extends HttpServlet {
    private static final String SEARCH_PRODUCTS_FORM = "/WEB-INF/jsp/catalog/SearchProducts.jsp";

    private CatelogService catelogService;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String keyword=req.getParameter("keyword");
        List<Product> productList=new ArrayList<>();
        if(keyword!=""){
            catelogService=new CatelogService();
            productList=catelogService.searchProductList(keyword);
        }


        HttpSession session=req.getSession();
        session.setAttribute("productList",productList);
        req.getRequestDispatcher(SEARCH_PRODUCTS_FORM).forward(req,resp);
    }
}
